import global from "../../global";
import Big, { Buff, Evolution, Spell } from "./big";
import Player from "./player";
import Small from "./small";

/**
 * 印记类型
 */
export enum MarkType{
    AIM,        //炮弹瞄准
    BLOODY,     //地狱杀伐
    VARIABLE,   //诡变无常
    QUAKE,      //敲山震虎
    MAZE,       //九曲黄河(本来准备弄一个迷宫的，让他走不出去)
    FOG,        //迷雾森林
    MERCY,      //佛法慈悲
    SNOW,       //风雪冰霜
}

/**
 * 印记
 */
export class Mark{
    constructor(id:number,type:MarkType){
        this.id = id;
        this.type = type;
    }
    id:number;          //谁施加的
    time:number = 3;    //阵法都只能存在3回合
    type:MarkType;      //印记类型
    node:cc.Node;       //印记节点
    zf_node:cc.Node;    //阵法节点
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class Point_ts extends cc.Component {

    @property(cc.SpriteFrame)                               //0  1  2  3  4  5
    Arr_sprite:cc.SpriteFrame[] = [];       //地形的图片数组  空/山/水/林/沙/炸弹
    @property(cc.SpriteFrame)                           
    Arr_gray:cc.SpriteFrame[] = [];         //地形的图片数组  空/山/水/林/沙/炸弹
    
    @property(cc.Node)
    Node_tip:cc.Node = null;                  //点击提示
    
    @property(cc.Sprite)
    Sprite:cc.Sprite = null;                //放地形图片的地方


    index:number = 0;   //索引
    x:number = 0;       //坐标
    y:number = 0;

    fake_type:number;   //记忆中虚假的地形
    real_type:number;   //真实地形

    is_view = true;    //是否拥有此地视野

    arr_mark:Set<Mark> = new Set<Mark>();  //全部印记

    onLoad(){
        this.Node_tip.active = false;
        this.Node_tip.parent = global.mgr.Layer_tip;
    }

    //初始化
    init(type:number,index:number){
        this.index = index;
        this.x = Math.floor(this.index/20);
        this.y = this.index%20;
        this.real_type = this.fake_type = type;
        this.setPosition()
        this.display();
    }
    
    /**
     * 根据index,确定自己的位置 
     */
    setPosition(){
        this.node.x = 200*this.x+100;
        this.node.y = 200*this.y+100;
        this.Node_tip.setPosition(this.node.position);
    }

    /**
     * 取消全部该落点事件监听
     */
    offAll(){
        //取消事件监听
        this.node.targetOff(this);
        //关闭提示效果
        this.closeEffect();
    }

    /**
     * 开启点击提示特效 0：可以行动，1：可出出大，2：可以移动，3：可以攻击，4：可以施法
     */
    openEffect(type:number = 0){
        if(global.is_replay){
            return;
        }
        this.Node_tip.getComponent(cc.Sprite).spriteFrame = global.effect.Arr_sf_tip[type];
        this.Node_tip.active = true;
        this.Node_tip.opacity = 255;
        if(type==1){
            this.Node_tip.color = global.mgr.me_player.color;
        }else{
            this.Node_tip.color = cc.color(255,255,255);
        }
        let action = cc.tween().to(1,{opacity:100}).delay(.2).to(1,{opacity:255});
        cc.tween(this.Node_tip).repeatForever(action).start();
    }
    /**
     * 关闭点击提示特效
     */
    closeEffect(){
        this.Node_tip.active = false;
        this.Node_tip.stopAllActions();
    }

    /**
     * 根据地形显示图片
     */
    display(flag:boolean = false){
        if(flag||this.is_view){
            //把记忆留下来
            this.fake_type = this.real_type;
            //图片显示
            switch(this.real_type){
                case 0:this.Sprite.spriteFrame = this.Arr_sprite[0];break;
                case 1:this.Sprite.spriteFrame = this.Arr_sprite[1];break;
                case 2:this.Sprite.spriteFrame = this.Arr_sprite[2];break;
                case 3:this.Sprite.spriteFrame = this.Arr_sprite[3];break;
                case 4:this.Sprite.spriteFrame = this.Arr_sprite[4];break;
                default:console.error("没有这种地形");
            }
            //大子展示
            let big =this.node.getChildByName("大子");
            if(big){
                big.getComponent(Big).show(flag);
            }
        }else{
            switch(this.fake_type){
                case 0:this.Sprite.spriteFrame = this.Arr_gray[0];break;
                case 1:this.Sprite.spriteFrame = this.Arr_gray[1];break;
                case 2:this.Sprite.spriteFrame = this.Arr_gray[2];break;
                case 3:this.Sprite.spriteFrame = this.Arr_gray[3];break;
                case 4:this.Sprite.spriteFrame = this.Arr_gray[4];break;
                default:console.error("没有这种地形");
            }
            //大子隐藏
            let big =this.node.getChildByName("大子");
            if(big){
                big.getComponent(Big).hide();
            }
        }
    }

    /**
     * 落小子
     */
    onSmallDown(){
        //判断是否可以落小子，条件满足开启监听
        if(this.real_type!=0){  //地形判断
            return;
        }
        if(this.node.getChildByName("小子")){   //已经被占领判断
            return;
        }
        //开启落小监听
        this.node.on(cc.Node.EventType.TOUCH_START,this.requestSmallDownYes,this);
        this.openEffect(1);
    }
    requestSmallDownYes(){
        let fake_small_ts = cc.instantiate(global.mgr.Prefab_small).getComponent(Small);
        fake_small_ts.Sprite.node.opacity = 150;
        fake_small_ts.Sprite.node.color = global.mgr.me_player.color;
        this.node.addChild(fake_small_ts.node);
        let obj = {method:"smallDown",id:global.id,index:this.index};
        global.frame.openFrameSmallDown(obj,fake_small_ts.node);
        //关闭落小子事件监听
        global.mgr.map_point.forEach((element)=>{
            element.offAll();
        });
    }

    
    /**
     * 小子出大子
     */
    onSmallBorn() {
        if(this.fake_type==1){
            return;
        }
        if(this.fake_type==2){
            return;
        }
        if(this.node.getChildByName("小子")){
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big&&end_big.active){     //有可见的大子
            return;
        }
        this.openEffect(1);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendSmallBorn,this);
    }
    sendSmallBorn(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"smallBorn",id:global.id,index:this.index});
    }
    smallBorn(player:Player){
        let cost = 2;
        this.is_me = player.is_me;
        //执行出大
        if(player.action<cost){
            this.fail(`需要${cost}点行动力`);
            return;
        }
        player.changeAction(-cost);
        //地形不对
        if(this.real_type==1||this.real_type==2){
            this.fail("地形杀");
            return;
        }
        //失败
        if(this.node.getChildByName("大子")){
            let big_ts = this.node.getChildByName("大子").getComponent(Big);
            big_ts.is_hide = false;
            this.fail("有敌人，失败");
            return;
        }
        //成功
        let big = cc.instantiate(global.mgr.Prefab_big);
        let big_ts = big.getComponent(Big);
        big_ts.Sprite.node.color = player.color;
        big_ts.player= player;
        big_ts.Jian.color = player.color;
        this.node.addChild(big);
        big_ts.init(this);
        player.arr_big.add(big_ts)
        //播放音效
        if(global.is_replay||global.mgr.canPlayEffect(this,player)){
            global.music_ts.playEffect("Audio_born");
        }
    }

    /**
     * 执行失败提示
     */
    is_me:boolean = false;
    fail(s:string){
        if(this.is_me){
            //消息提示
            global.persist.tip(s);
            //地形探查
            this.fake_type = this.real_type;
            this.display();
        }
    }
    
    /**
     * 大子出大子
     */
    onBigBorn(){
        if(this.fake_type==1){
            return;
        }
        if(this.fake_type==2){
            return;
        }
        if(this.node.getChildByName("小子")){
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big&&end_big.active){     //有可见的大子
            return;
        }
        this.openEffect(1);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendBigBorn,this);
    }
    sendBigBorn(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"bigBorn",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index});
    }
    bigBorn(player:Player,start:number,is_gdfl:boolean = false){
        let cost = 2;
        let start_bigTs = global.mgr.map_point.get(start).node.getChildByName("大子").getComponent(Big);
        this.is_me = player.is_me;
        //是否有桃李天下
        if(start_bigTs.arr_spell.has(Spell.TLTX)){
            cost = 1;
        }
        //地形不对
        if(this.real_type==1||this.real_type==2){
            this.fail("地形杀");
            return;
        }
        //失败
        if(this.node.getChildByName("大子")){
            let big_ts = this.node.getChildByName("大子").getComponent(Big);
            start_bigTs.is_hide = false;
            big_ts.is_hide = false;
            player.changeAction(-cost);
            this.fail("有敌人，失败");
            return;
        }
        if(player.action<cost){
            this.fail(`需要${cost}点行动力`);
            return;
        }
        player.changeAction(-cost);
        //成功
        let big = cc.instantiate(global.mgr.Prefab_big);
        let big_ts = big.getComponent(Big);
        big_ts.Sprite.node.color = player.color;
        big_ts.player= player;
        big_ts.father = start_bigTs;
        big_ts.Jian.color = player.color;
        this.node.addChild(big);
        big_ts.init(this);
        player.arr_big.add(big_ts)
        if(is_gdfl){
            big_ts.arr_buff.add(Buff.ice);
        }
        //播放音效
        if(global.is_replay||global.mgr.canPlayEffect(this,player)){
            global.music_ts.playEffect("Audio_born");
        }
    }

    /**
     * 大子移动 is_water 表示会不会水
     */
    onBigMove(is_water:boolean = false){
        if(this.fake_type==1){
            return;
        }
        if(this.fake_type==2&&!is_water){
            return;
        }
        if(this.node.getChildByName("小子")){
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big&&end_big.getComponent(Big).player==global.mgr.me_player){     //自己
            return;
        }
        this.openEffect(2);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendBigMove,this);
    }
    sendBigMove(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"bigMove",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index});
    }
    bigMove(player:Player,start:number){
        let cost = 1;
        let big_ts = global.mgr.map_point.get(start).node.getChildByName("大子").getComponent(Big);
        this.is_me = player.is_me;
        if(this.real_type==4){
            cost = 2;
            if(big_ts.arr_spell.has(Spell.SMWZ)){
                cost = 1;
            }
        }
        //执行
        if(player.action<cost){
            this.fail(`需要${cost}点行动力`);
            return;
        }
        player.changeAction(-cost);
        //地形不对
        if(this.real_type==1||(this.real_type==2&&!big_ts.arr_spell.has(Spell.CFPL))){
            this.fail("地形杀");
            return;
        }
        //失败
        let end_big = this.node.getChildByName("大子");
        if(end_big){
            let end_bigTs = end_big.getComponent(Big);
            //我是潜行捕手
            if(big_ts.arr_spell.has(Spell.QXBS)){
                if(end_bigTs.is_hide){
                    player.changeAction(6);
                    this.attack(player,big_ts,end_big);
                    return;
                }
            }
            end_bigTs.is_hide = false;
            big_ts.die()
            return; 
        }
        //成功
        big_ts.node.setParent(this.node);
        big_ts.init(this);
        //沙漠王子改地形
        if(big_ts.arr_spell.has(Spell.SMWZ)){
            this.real_type = 4;
            big_ts.is_hide = false;
        }
        //佛法慈悲给金身,风雪冰霜给冰冻
        this.arr_mark.forEach(mark=>{
            if(mark.type==MarkType.MERCY&&mark.id==big_ts.player.id){
                big_ts.arr_buff.add(Buff.gold);
            }else if(mark.type==MarkType.SNOW&&mark.id!=big_ts.player.id){
                big_ts.arr_buff.add(Buff.ice);
            }
        });
        //这里定义一个变量 步步生莲
        let bbsl = false;
        //天御圣灯，周围大子获得金身
        if(big_ts.arr_spell.has(Spell.TYSD)){          
            let arr = global.getAround(this.index);
            arr.forEach(i=>{
                let temp_big = global.mgr.map_point.get(i).node.getChildByName("大子");
                if(temp_big){
                    let temp_big_ts = temp_big.getComponent(Big);
                    if(temp_big_ts.player.id==big_ts.player.id&&temp_big_ts.evolution==Evolution.Null){
                        temp_big_ts.arr_buff.add(Buff.gold);
                        bbsl = true;
                    }
                }
            });
        }
        //万厄毒体，周围敌方大子获得瘟疫
        if(big_ts.arr_spell.has(Spell.WEDT)){          
            let arr = global.getAround(this.index);
            arr.forEach(i=>{
                let temp_big = global.mgr.map_point.get(i).node.getChildByName("大子");
                if(temp_big){
                    let temp_big_ts = temp_big.getComponent(Big);
                    if(temp_big_ts.player.id!=big_ts.player.id&&temp_big_ts.evolution==Evolution.Null){
                        temp_big_ts.arr_buff.add(Buff.virus);
                        bbsl = true;
                    }
                }
            });
        }
        //拆炮专家
        if(big_ts.arr_spell.has(Spell.CPZJ)){          
            let arr = global.getAround(this.index);
            arr.forEach(i=>{
                let temp_big = global.mgr.map_point.get(i).node.getChildByName("大子");
                if(temp_big){
                    let temp_big_ts = temp_big.getComponent(Big);
                    if(temp_big_ts.player.id!=big_ts.player.id&&temp_big_ts.evolution==Evolution.Battery){
                        temp_big_ts.die();
                        bbsl = true;
                    }
                }
            });
        }
        //捉鬼大师
        if(big_ts.arr_spell.has(Spell.ZGDS)){          
            let arr = global.getAround(this.index);
            arr.forEach(i=>{
                let temp_big = global.mgr.map_point.get(i).node.getChildByName("大子");
                if(temp_big){
                    let temp_big_ts = temp_big.getComponent(Big);
                    if(temp_big_ts.player.id!=big_ts.player.id&&temp_big_ts.evolution==Evolution.Ghost){
                        temp_big_ts.die();
                        player.changeAction(2);
                        bbsl = true;
                    }
                }
            });
        }
        //键校正
        player.arr_big.forEach(big_ts=>{
            big_ts.showJian();
        });
        //播放音效
        if(global.is_replay||global.mgr.canPlayEffect(this,player)){
            global.music_ts.playEffect("Audio_move");
            if(bbsl){
                global.music_ts.playEffect("Audio_moveBBSL");
                global.effect.playBBSL(this.node.position);
            }
        }
    }

    /**
     * 大子攻击
     */
    onBigAttack(is_water:boolean = false){
        if(this.fake_type==1){
            return;
        }
        if(this.fake_type==2&&!is_water){
            return;
        }
        let small = this.node.getChildByName("小子");
        if(small&&small.getComponent(Small).player==global.UI.big_ts.player){   
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big){     
            let end_bigTs = end_big.getComponent(Big);
            if(end_bigTs.player==global.UI.big_ts.player){      //自己
                return;
            }
            if(end_bigTs.evolution==Evolution.Bone2||end_bigTs.evolution==Evolution.Bone1){     //顽石
                return;
            }
        }
        this.openEffect(3);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendBigAttack,this);
    }
    sendBigAttack(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"bigAttack",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index});
    }
    bigAttack(player:Player,start:number){
        let start_bigTs = global.mgr.map_point.get(start).node.getChildByName("大子").getComponent(Big);
        this.is_me = player.is_me;
        //判断攻击类型
        let end_big = this.node.getChildByName("大子");
        let end_small = this.node.getChildByName("小子"); 
        if(end_small){
            //吃小子
            this.eat(player,start_bigTs,end_small);
        }else{
            //打大子
            this.attack(player,start_bigTs,end_big);
        }
    }
    attack(player:Player,start_bigTs:Big,end_big:cc.Node){
        //减行动力
        let cost = 2;   
        if(this.real_type==4){
            cost = 4;
            if(start_bigTs.arr_spell.has(Spell.SMWZ)){
                cost = 2;
            }
        }
        //检查是否有效果可以减少攻击消耗
        if(start_bigTs.arr_spell.has(Spell.XYSZ)){
            cost--;
        }
        this.arr_mark.forEach(mark=>{
            if(mark.type==MarkType.BLOODY){
                if(player.id==mark.id){
                    cost = 0;
                }
            }
        });
        if(player.action<cost){
            this.fail(`需要${cost}点行动力`);
            return;
        }
        player.changeAction(-cost);
        //地形不对
        if(this.real_type==1||(this.real_type==2&&!start_bigTs.arr_spell.has(Spell.CFPL))){
            this.fail("地形杀");
            return;
        }
        //没有敌人
        if(!end_big){
            this.fail("没有敌人");
            return;
        }
        //有敌人
        let end_bigTs = end_big.getComponent(Big);
        //攻击出去了，被攻击大子返回true,表示自己死亡了
        if(end_bigTs.attacked(start_bigTs)){
            start_bigTs.node.setParent(this.node);
            start_bigTs.init(this);
            start_bigTs.is_hide = false;
            //键校正
            player.arr_big.forEach(big_ts=>{
                big_ts.showJian();
            });
            //播放音效
            if(global.is_replay||global.mgr.canPlayEffect(this,player)){
                global.music_ts.playEffect("Audio_attack");
            }
        }else{
            start_bigTs.is_hide = false;
            //攻击失败，我不是金身则死亡
            if(!start_bigTs.arr_buff.has(Buff.gold)){
                start_bigTs.die();
            }
        }
    }   
    eat(player:Player,start_bigTs:Big,end_small:cc.Node){
        //减行动力
        let cost = 2;   
        //检查是否有效果可以减少攻击消耗
        if(start_bigTs.arr_spell.has(Spell.XYSZ)){
            cost--;
        }
        this.arr_mark.forEach(mark=>{
            if(mark.type==MarkType.BLOODY){
                if(player.id==mark.id){
                    cost = 0;
                }
            }
        });
        if(player.action<cost){
            this.fail(`需要${cost}点行动力`);
            return;
        }
        player.changeAction(-cost);
        //成功攻击
        start_bigTs.node.setParent(this.node);
        start_bigTs.init(this);
        start_bigTs.is_hide = false;
        //键校正
        player.arr_big.forEach(big_ts=>{
            big_ts.showJian();
        });
        //播放音效
        global.music_ts.playEffect("Audio_attack");
        end_small.getComponent(Small).killed();
    }

    /**
     * 幽灵移动
     */
    onGhostMove(){
        if(this.node.getChildByName("小子")){   //小子不能走
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big){    
            let end_bigTs = end_big.getComponent(Big);
            if(end_bigTs.player.is_me){     //己方大子不能走
                return;
            }
            if(end_bigTs.evolution!=Evolution.Null){    //转型过的不能走
                return;
            }
        }
        this.openEffect(2);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendGhostMove,this);
    }
    sendGhostMove(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"ghostMove",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index});
    }
    ghostMove(player:Player,start:number){
        let big_ts = global.mgr.map_point.get(start).node.getChildByName("大子").getComponent(Big);
        this.is_me = player.is_me;
        //有敌方大子就死亡
        let end_big = this.node.getChildByName("大子");
        if(end_big){
            //正常情况是要鬼死的，如果这个鬼有 恶灵附身，则自己要叛变
            if(big_ts.arr_spell.has(Spell.ELFS)){
                end_big.getComponent(Big).doelfs(player,big_ts.father);
                end_big.getComponent(Big).arr_buff.add(Buff.ice);
                this.fail("恶灵附身");
                big_ts.node.setParent(this.node);
                big_ts.init(this);
                big_ts.die(true);
            }else{
                end_big.getComponent(Big).is_hide = false;
                end_big.getComponent(Big).arr_buff.add(Buff.fear);
                this.fail("有敌方大子");
                big_ts.node.setParent(this.node);
                big_ts.init(this);
                big_ts.die();
            }
            return; 
        }
        //先移动过去
        big_ts.node.setParent(this.node);
        big_ts.init(this);
        //键校正
        player.arr_big.forEach(big_ts=>{
            big_ts.showJian();
        });
        //播放音效
        if(global.is_replay||global.mgr.canPlayEffect(this,player)){
            global.music_ts.playEffect("Audio_ghost");
        }
    }

    /**
     * 炮台攻击
     */
    onBatteryAttack(){
        //判断是否可以允许攻击
        let small = this.node.getChildByName("小子");
        if(small&&small.getComponent(Small).player.is_me){   
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big){     
            if(end_big.getComponent(Big).player==global.mgr.me_player){             //自己
                return;
            }
            let big_ts = end_big.getComponent(Big);
            if(big_ts.evolution==Evolution.Bone1||big_ts.evolution==Evolution.Bone2){   //不能打顽石
                return;
            }
        }
        this.openEffect(3);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendBatteryAttack,this);
    }
    sendBatteryAttack(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"batteryAttack",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index}); 
    }
    batteryAttack(start:number){
        let start_pointTs = global.mgr.map_point.get(start);
        let end_big = this.node.getChildByName("大子");
        let end_small = this.node.getChildByName("小子");
        //开炮
        //播放音效
        if(global.is_replay||global.mgr.me_player.is_surrender||start_pointTs.is_view||this.is_view){
            global.effect.playShell(start_pointTs.node.position,this.node.position);
        }
        //有大子
        if(end_big){
            let end_bigTs = end_big.getComponent(Big);
            this.scheduleOnce(()=>{end_bigTs.attacked();global.mgr.refreshView();},.8);
            return
        }
        //有小子
        if(end_small){
            let smallTs = end_small.getComponent(Small);
            this.scheduleOnce(()=>{smallTs.killed();},.8);
        }
        
    }

    /**
     * 地灵推土
     */
    onKoboldMagic(){
        //判断是否可以允许转地
        if(this.node.getChildByName("小子")){   //小子
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big&&end_big.getComponent(Big).player==global.mgr.me_player){     //自己
            return;
        }
        this.openEffect(2);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendKoboldMagic,this);
    }
    sendKoboldMagic(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"koboldMagic",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index}); 
    }
    koboldMagic(start:number,player:Player){
        let start_big = global.mgr.map_point.get(start).node.getChildByName("大子").getComponent(Big);
        let end_big = this.node.getChildByName("大子");
        //失败
        start_big.node.setParent(this.node);
        start_big.init(this);
        start_big.showJian();
        if(end_big){
            start_big.die();
            return;
        }
        //成功
        this.real_type = 0;
        //播放音效
        if(global.is_replay||global.mgr.canPlayEffect(this,player)){
            global.music_ts.playEffect("Audio_kobold");
        }
    }

    /**
     * 狙炮瞄准
     */
    onBatteryAim(){
        if(this.node==global.UI.big_ts.node.parent){    //不能是自己打自己
            return;
        }
        this.openEffect(3);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendBatteryAim,this);
    }
    sendBatteryAim(){
        if(global.mgr.me_player.action==0){
            global.persist.tip("开炮需要1点行动力");
            global.mgr.pointOffAll();
            return;
        }
        global.UI.big_ts.is_canJP = false;
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"batteryAim",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index});
    }
    /**
     * 狙炮打击
     */
    JPHit(id:number){
        let arr = global.getAround(this.index);
        arr.push(this.index);
        arr.forEach(index=>{
            let point_ts = global.mgr.map_point.get(index);
            let big = point_ts.node.getChildByName("大子");
            if(big){
                let big_ts = big.getComponent(Big);
                if(big_ts.player.id!=id){
                    big_ts.attacked();
                }
            }
        });
    }

    /**
     * 超鬼移动
     */
    onCGMove(){
        if(this.node.getChildByName("小子")){   //小子
            return;
        }
        let end_big = this.node.getChildByName("大子");
        if(end_big){        //有大子
            let big_ts = end_big.getComponent(Big);
            if(big_ts.player.id==global.id){      //自己的大子不行
                return;
            }
        }
        this.openEffect(2);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendCGMove,this);
    }
    sendCGMove(){
        global.mgr.pointOffAll();
        global.client.send({method:"operation",operation:"CGMove",id:global.id,start:global.UI.big_ts.node.parent.getComponent(Point_ts).index,end:this.index});
    }
    CGMove(player:Player,start:number){
        let big_ts = global.mgr.map_point.get(start).node.getChildByName("大子").getComponent(Big);
        this.is_me = player.is_me;
        //有敌方大子
        let end_big = this.node.getChildByName("大子");
        if(end_big){
            let end_bigTs = end_big.getComponent(Big);
            if(end_bigTs.arr_buff.has(Buff.fear)||end_bigTs.evolution==Evolution.Ghost){  //对方是鬼或者怯战
                if(!end_bigTs.attacked(big_ts)){
                    big_ts.node.setParent(this.node);
                    big_ts.init(this);
                    big_ts.die();
                    end_big.getComponent(Big).is_hide = false;
                    this.fail("有敌方大子");
                    return;  
                }
            }else{
                //正常情况是要鬼死的，如果这个鬼有 恶灵附身，则自己要叛变
                if(big_ts.arr_spell.has(Spell.ELFS)){
                    end_bigTs.doelfs(player,big_ts.father);
                    end_bigTs.arr_buff.add(Buff.ice);
                    this.fail("恶灵附身");
                    big_ts.node.setParent(this.node);
                    big_ts.init(this);
                    big_ts.die(true);
                }else{
                    end_big.getComponent(Big).is_hide = false;
                    end_big.getComponent(Big).arr_buff.add(Buff.fear);
                    this.fail("有敌方大子");
                    big_ts.node.setParent(this.node);
                    big_ts.init(this);
                    big_ts.die();
                }
                return;  
            }
        }
        //先移动过去
        big_ts.node.setParent(this.node);
        big_ts.init(this);
        //键校正
        player.arr_big.forEach(big_ts=>{
            big_ts.showJian();
        });
        //播放音效
        if(global.is_replay||global.mgr.canPlayEffect(this,player)){
            global.music_ts.playEffect("Audio_ghost");
        }
    }


    /**
     * 地狱杀伐
     */
    ondysf(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.senddysf,this);
    }
    senddysf(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"dysf",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 冰冻手雷
     */
    onbdsl(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.bdsl,this);
    }
    bdsl(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"bdsl",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }


    /**
     * 诡变无常
     */
    ongbwc(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendgbwc,this);
    }
    sendgbwc(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"gbwc",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }
    n_dx:number = 0;    //地形累加
    gbwc(id:number){
        //有小子则地形不变
        if(this.node.getChildByName("小子")){
            return;
        }
        //有己方大子则不变
        let big = this.node.getChildByName("大子");
        let big_ts = null;
        if(big){
            big_ts = big.getComponent(Big);
            if(big_ts.player.id==id){
                return;
            }
        }
        //开始变
        let i = Math.floor(this.real_type+this.index+(this.n_dx++)*3)%4;        //随便写的假随机，一个公式能保证玩家的地图显示相同，不能变 沙
        this.real_type = i;
        //大子不适应则死亡
        if(big){
            if(this.real_type==1){
                if(big_ts.evolution!=Evolution.Ghost){
                    big_ts.die();
                    return ;
                }
            }else if(this.real_type==2){
                if(!(big_ts.evolution==Evolution.Ghost||big_ts.arr_spell.has(Spell.CFPL))){
                    big_ts.die();
                    return ;
                }
            }
            big_ts.init(this);
        }
    }

    /**
     * 敲山震虎
     */
    onqszh(){
        if(this.real_type!=1){
            return;
        }
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendqszh,this);
    }
    sendqszh(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"qszh",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 搬山大法
     */
    onbsdf(){
        if(this.real_type!=1){
            return;
        }
        if(this.node.getChildByName("大子")){
            return;
        }
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.bsdf1,this);
    }
    bsdf1(){
        global.mgrSkill.bsdf = this;
        global.mgr.clear();
        let arr_pointTs = global.mgr.getArrPointView();
        arr_pointTs.forEach(point_ts=>{
            point_ts.onbsdf2();
        });
        this.offAll();  //不算自己
    }
    onbsdf2(){
        if(this.real_type!=0){
            return;
        }
        if(this.node.getChildByName("大子")||this.node.getChildByName("小子")){
            return;
        }
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.bsdf2,this);
    }
    bsdf2(){
        global.client.send({method:"skill",skill:"bsdf",id:global.id,index1:global.mgrSkill.bsdf.index,index2:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 九曲黄河
     */
    onjqhh(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendjqhh,this);
    }
    sendjqhh(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"jqhh",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 佛法慈悲
     */
    onffcb(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendffcb,this);
    }
    sendffcb(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"ffcb",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 迷雾森林
     */
    onmwsl(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendmwsl,this);
    }
    sendmwsl(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"mwsl",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 迷雾森林
     */
    onfxbs(){
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendfxbs,this);
    }
    sendfxbs(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"fxbs",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 神兵天降
     */
    onsbtj(){
        if(this.real_type!=0){
            return;
        }
        if(this.node.getChildByName("大子")||this.node.getChildByName("小子")){
            return;
        }
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendsbtj,this);
    }
    sendsbtj(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"sbtj",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

    /**
     * 狡兔三窟
     */
    onjtsk(){
        if(this.real_type!=0){
            return;
        }
        if(this.node.getChildByName("大子")||this.node.getChildByName("小子")){
            return;
        }
        this.openEffect(4);
        this.node.on(cc.Node.EventType.TOUCH_START,this.sendjtsk,this);
    }
    sendjtsk(){
        global.mgr.pointOffAll();
        global.client.send({method:"skill",skill:"jtsk",id:global.id,index:this.index});
        global.mgr.skill_ts.used();
    }

}
